home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4261 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: howland.reston.ans.net!torn!sq!msb
  3. From: msb@sq.com (Mark Brader)
  4. Subject: Re: What's wrong here?
  5. Message-ID: <1996Feb2.214358.10906@sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <4eml5o$o6h@airdmhor.gen.nz> <310FB5FF.4BE6@microsports.com>
  8. Date: Fri, 2 Feb 1996 21:43:58 GMT
  9.  
  10. > You may be able to clear everything up by changing lines 9-11 to:
  11. >   a = (word) 1;
  12. >   b = (word) 2;
  13. >   c = (word) 3;
  14.  
  15. Considering that the spurious warnings begin on line 12, this seems
  16. *rather* unlikely.  If a workaround of this kind is to be applied,
  17. it would be to change line 12 from
  18.  
  19.     d = a | b | c;
  20. to
  21.     d = (word) (a | b | c);
  22.  
  23. Of course, even with the redundant cast in place, the type of the
  24. right-hand side of the = sign is *still* int here.  What this line
  25. now specifies, assuming that "word" (typedef'ed unsigned short) is
  26. narrower than int, is:
  27.  
  28.      1. Widen the values of a and b to int
  29.      2. "Or" those two values together, producing an int
  30.      3. Widen the value of c to int [this step may be done earlier]
  31.      4. "Or" the two values from steps 2 and 3 together, producing an int
  32.      5. Narrow this int to unsigned short
  33.      6. Widen this unsigned short to int
  34.      7. Narrow this int to unsigned short
  35.      8. Assign this unsigned short to d
  36.      9. Widen the unsigned short to int
  37.     10. Discard the int
  38.  
  39. where step 5 is the cast, but its presence merely causes step 6 to be
  40. inserted before the automatic conversion of step 7.  In practice, of
  41. course, steps 5-7 will be optimized down to a single conversion, so
  42. the cast will have exactly no effect.
  43.  
  44. But a compiler that would issue a warning for a line as innocuous as
  45. this might well be looking for exactly this change.
  46. -- 
  47. Mark Brader              |  "One of the lessons of history is that nothing
  48. msb@sq.com               |   is often a good thing to do and always a clever
  49. SoftQuad Inc., Toronto   |   thing to say."               -- Will Durant
  50.  
  51. My text in this article is in the public domain.
  52.